<script setup>
import { ref } from 'vue'
import Carousel from '../components/Carousel.vue'
import ProductCard from '../components/ProductCard.vue'
import { injectLanguage } from '../utils/language-store.js'

// 使用语言服务
const { t } = injectLanguage()

// 轮播图数据
const slides = [
  {
    image: 'https://picsum.photos/1200/500?random=1',
    title: '创新技术驱动未来',
    description: '我们致力于通过技术创新推动行业发展'
  },
  {
    image: 'https://picsum.photos/1200/500?random=2',
    title: '专业团队保驾护航',
    description: '经验丰富的团队为您提供专业服务'
  },
  {
    image: 'https://picsum.photos/1200/500?random=3',
    title: '客户至上服务为本',
    description: '以客户需求为中心，提供定制化解决方案'
  }
]

// 产品数据
const products = [
  {
    id: 1,
    name: '产品设计',
    description: '我们提供创新的产品设计服务，满足您的各种需求。',
    price: '¥5000起',
    image: 'https://picsum.photos/400/300?random=4',
    buttonText: '了解更多'
  },
  {
    id: 2,
    name: '技术开发',
    description: '我们的技术团队拥有丰富的经验，能够实现复杂的系统开发。',
    price: '¥10000起',
    image: 'https://picsum.photos/400/300?random=5',
    buttonText: '查看详情'
  },
  {
    id: 3,
    name: '售后服务',
    description: '我们提供全面的售后服务，确保客户满意度。',
    price: '¥2000起',
    image: 'https://picsum.photos/400/300?random=6',
    buttonText: '咨询详情'
  }
]

// 客户案例数据
const caseStudies = [
  {
    id: 1,
    title: '电商平台开发',
    description: '成功为某大型零售企业开发了高性能电商平台，支持日均百万级访问量。'
  },
  {
    id: 2,
    title: '金融风控系统',
    description: '为某知名金融机构构建了智能风控系统，显著降低了风险损失。'
  },
  {
    id: 3,
    title: '移动办公应用',
    description: '帮助某跨国企业实现了移动办公解决方案，提升了员工协作效率。'
  }
]

// 在线客服相关
const isChatOpen = ref(false)
const messages = ref([])
const newMessage = ref('')

const toggleChat = () => {
  isChatOpen.value = !isChatOpen.value
}

const sendMessage = () => {
  if (newMessage.value.trim()) {
    messages.value.push({
      text: newMessage.value,
    sender: 'user',
      timestamp: new Date()
    })
    
    // 模拟客服回复
    setTimeout(() => {
      messages.value.push({
        text: t('chat.agentReply'),
        sender: 'agent',
        timestamp: new Date()
      })
    }, 1000)
    
    newMessage.value = ''
  }
}

const handleProductClick = (product) => {
  console.log('产品被点击:', product)
  // 这里可以添加跳转到产品详情页的逻辑
}
</script>

<template>
  <div class="home">
    <!-- 轮播图区域 -->
    <section class="hero">
      <Carousel :slides="slides" />
    </section>

    <!-- 公司介绍 -->
    <section class="company-intro">
      <div class="container">
        <h2 v-text="t('home.companyIntro')"></h2>
        <p v-text="t('home.companyDesc')"></p>
      </div>
    </section>

    <!-- 产品服务展示 -->
    <section class="services">
      <div class="container">
        <h2 v-text="t('home.ourServices')"></h2>
        <div class="service-cards">
          <ProductCard 
            v-for="product in products" 
            :key="product.id"
            :product="product" 
            @click="handleProductClick"
          />
        </div>
      </div>
    </section>

    <!-- 客户案例 -->
    <section class="cases">
      <div class="container">
        <h2 v-text="t('home.customerCases')"></h2>
        <div class="case-studies">
          <div class="case-card" v-for="(caseStudy, index) in caseStudies" :key="caseStudy.id">
            <h3 v-text="t(`home.case${index+1}Title`)"></h3>
            <p v-text="t(`home.case${index+1}Desc`)"></p>
          </div>
        </div>
      </div>
    </section>


    <!-- 在线客服 -->
    <div class="chat-container">
      <div
          class="chat-toggle"
          @click="toggleChat"
          :class="{ 'open': isChatOpen }"
      >
        <span v-if="!isChatOpen" v-text="t('chat.title')"></span>
        <span v-else v-text="t('chat.collapse')"></span>
      </div>
      <div
          class="chat-box"
          v-if="isChatOpen"
      >
        <div class="chat-header">
          <h3 v-text="t('chat.title')"></h3>
          <button @click="toggleChat">×</button>
        </div>
        <div class="chat-messages">
          <div
              v-for="(message, index) in messages"
              :key="index"
              :class="['message', message.sender]"
          >
            <div class="message-content" v-text="message.text">
            </div>
            <div class="message-time" v-text="message.timestamp.toLocaleTimeString()">
            </div>
          </div>
        </div>
        <div class="chat-input">
          <input
              v-model="newMessage"
              @keyup.enter="sendMessage"
              :placeholder="t('chat.placeholder')"
          >
          <button @click="sendMessage" v-text="t('chat.send')"></button>
        </div>
      </div>
    </div>
  </div>
</template>


<style scoped>
.home {
  font-family: 'Arial', sans-serif;
}

.hero {
  text-align: center;
  padding: 6rem 2rem;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  margin-bottom: 3rem;
  border-radius: 0 0 30px 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
}

.hero h1 {
  font-size: 3.5rem;
  margin-bottom: 1.5rem;
  font-weight: 800;
  letter-spacing: -0.5px;
}

.hero p {
  font-size: 1.3rem;
  margin-bottom: 2.5rem;
  opacity: 0.95;
  max-width: 700px;
  margin-left: auto;
  margin-right: auto;
}

.hero button {
  background: white;
  color: #667eea;
  border: none;
  padding: 1.2rem 2.5rem;
  font-size: 1.2rem;
  font-weight: 600;
  border-radius: 50px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.2);
}

.hero button:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 50px 20px;
}

.company-intro {
  background-color: #f9f9f9;
  text-align: center;
}

.company-intro h2, .services h2, .cases h2 {
  text-align: center;
  font-size: 2.8rem;
  margin-bottom: 3.5rem;
  color: #333;
  position: relative;
}

.company-intro h2::after, .services h2::after, .cases h2::after {
  content: '';
  display: block;
  width: 80px;
  height: 4px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  margin: 1rem auto;
  border-radius: 2px;
}

.service-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 2.5rem;
  margin-bottom: 5rem;
}

.card {
  background: white;
  border-radius: 20px;
  padding: 2.5rem;
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.1);
  transition: all 0.4s ease;
  text-align: center;
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.card:hover {
  transform: translateY(-15px);
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
  border-color: rgba(102, 126, 234, 0.2);
}

.card img {
  width: 100%;
  height: 220px;
  object-fit: cover;
  border-radius: 15px;
  margin-bottom: 2rem;
  transition: transform 0.3s ease;
}

.card:hover img {
  transform: scale(1.03);
}

.card h3 {
  font-size: 1.7rem;
  margin-bottom: 1.2rem;
  color: #333;
  font-weight: 700;
}

.card p {
  color: #666;
  line-height: 1.7;
  font-size: 1.1rem;
}

.case-studies {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(380px, 1fr));
  gap: 2.5rem;
}

.case-card {
  background: white;
  border-radius: 20px;
  padding: 2.5rem;
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.case-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 20px 45px rgba(0, 0, 0, 0.12);
  border-color: rgba(102, 126, 234, 0.2);
}

.case-card h3 {
  font-size: 1.5rem;
  margin-bottom: 1.2rem;
  color: #333;
  font-weight: 700;
}

.case-card p {
  color: #666;
  line-height: 1.7;
  font-size: 1.1rem;
}

/* 在线客服样式 */
.chat-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 1000;
}

.chat-toggle {
  background: #667eea;
  color: white;
  border: none;
  padding: 12px 20px;
  border-radius: 25px;
  cursor: pointer;
  box-shadow: 0 2px 10px rgba(0,0,0,0.2);
  transition: all 0.3s ease;
}

.chat-toggle:hover {
  background: #5a6fd8;
}

.chat-toggle.open {
  border-radius: 25px 25px 0 0;
}

.chat-box {
  width: 350px;
  height: 400px;
  background: white;
  border-radius: 10px 10px 0 10px;
  box-shadow: 0 5px 15px rgba(0,0,0,0.2);
  display: flex;
  flex-direction: column;
  margin-top: 5px;
}

.chat-header {
  background: #667eea;
  color: white;
  padding: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-radius: 10px 10px 0 0;
}

.chat-header button {
  background: none;
  border: none;
  color: white;
  font-size: 24px;
  cursor: pointer;
}

.chat-messages {
  flex: 1;
  padding: 15px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.message {
  max-width: 70%;
  padding: 10px 15px;
  border-radius: 18px;
  font-size: 14px;
}

.message.user {
  align-self: flex-end;
  background: #667eea;
  color: white;
  border-bottom-right-radius: 5px;
}

.message.agent {
  align-self: flex-start;
  background: #f1f1f1;
  color: #333;
  border-bottom-left-radius: 5px;
}

.message-content {
  margin-bottom: 5px;
}

.message-time {
  font-size: 11px;
  opacity: 0.7;
  text-align: right;
}

.chat-input {
  display: flex;
  padding: 15px;
  border-top: 1px solid #eee;
}

.chat-input input {
  flex: 1;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 20px;
  outline: none;
}

.chat-input button {
  margin-left: 10px;
  padding: 10px 15px;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 20px;
  cursor: pointer;
}

@media (max-width: 768px) {
  .hero {
    padding: 4rem 1.5rem;
  }

  .hero h1 {
    font-size: 2.5rem;
  }

  .hero p {
    font-size: 1.1rem;
  }

  .container {
    padding: 3rem 1.5rem;
  }

  .company-intro h2, .services h2, .cases h2 {
    font-size: 2.2rem;
  }

  .service-cards, .case-studies {
    grid-template-columns: 1fr;
    gap: 2rem;
  }

  .chat-box {
    width: 300px;
    height: 350px;
  }
}
</style>